﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _021_第二十一章_单例模式
{
    /// <summary>
    /// 懒汉模式，单例
    /// </summary>
    class Singleton
    {
        private static Singleton instance;
        private static readonly object syncRoot = new object();

        public static Singleton Instance
        {
            get
            {
                if(instance == null) //双重锁定目的：外层判断，为了节约性能，不用每次上锁；内存判断，防止多线程同时访问
                {
                    lock (syncRoot)
                    {
                        if(instance == null)
                        {
                            instance = new Singleton();
                        }
                    }
                }
                return instance;
            }
        }

        private Singleton()
        {

        }

    }
}
